package com.fingard.dsp.bank.directbank.chinapay01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IGateway;
import com.fingard.dsp.bank.directbank.chinapay01.util.AcpService;
import com.fingard.dsp.bank.directbank.chinapay01.util.SDKUtil;
import com.fingard.dsp.bank.reqrespobj.gateway.ReqGateway;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.text.StringHelper;

/**
 * 中国银联网关支付
 * 
 * @author Cruise
 * @date 2018年8月21日
 */
public class ChinaPay01Gateway extends ChinaPay01Base implements IGateway {
	/**
	 * 银联网关支付下单
	 */
	@Override
	public HashMap<String, String> GenGwMap(ReqGateway p_reqGateway) throws Exception {
		HashMap<String, String> tmpRetMap = new HashMap<String, String>();

		try {
			String ownAct = p_reqGateway.reqHashMap.get("OurBankAccount");
			ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "gateway");
			if (actItem != null) {
				Map<String, String> tmpGatewayRequest = new HashMap<String, String>();
				
				tmpGatewayRequest.put("version", "5.1.0");
				tmpGatewayRequest.put("encoding", getCharset()); 
				tmpGatewayRequest.put("bizType", "000201");// 业务类型，B2C网关支付，手机wap支付
				tmpGatewayRequest.put("txnTime", Format.DateTime14Format.format(p_reqGateway.getTimeStart()));
				tmpGatewayRequest.put("backUrl", actItem.notifyURL);
				tmpGatewayRequest.put("currencyCode", "156");
				tmpGatewayRequest.put("txnAmt", StringHelper.amountMultiply100(p_reqGateway.reqHashMap.get("Amount"))); 
				tmpGatewayRequest.put("txnType", "01"); // 交易类型 ，01：消费
				tmpGatewayRequest.put("txnSubType", "01");// 交易子类型， 01：自助消费
				tmpGatewayRequest.put("accessType", "0");
				tmpGatewayRequest.put("signMethod", "01"); // 签名方法
				tmpGatewayRequest.put("channelType", "07"); // 渠道类型，这个字段区分B2C网关支付和手机wap支付；07：PC,平板
				tmpGatewayRequest.put("merId", actItem.merchantId);
				tmpGatewayRequest.put("orderId", p_reqGateway.reqHashMap.get("RdSeq"));
				tmpGatewayRequest.put("orderDesc", p_reqGateway.reqHashMap.get("Description"));
				tmpGatewayRequest.put("frontUrl", actItem.returnURL);
				tmpGatewayRequest.put("encryptCertId",getEncryptCertId(actItem));  
				
//				tmpGatewayRequest.put("orderTimeout", actItem.returnURL);
				tmpGatewayRequest.put("reqReserved", p_reqGateway.reqHashMap.get("Attach"));
				tmpGatewayRequest.put("payTimeout", Format.DateTime14Format.format(p_reqGateway.getTimeExpire()));
//				tmpGatewayRequest.put("termId", actItem.createIP);
				
				tmpGatewayRequest = SDKUtil.filterBlank(tmpGatewayRequest);
				String tmpSignData = SDKUtil.coverMap2String(tmpGatewayRequest);
				
				Map<String, String> tmpStrRequest = AcpService.signByCertInfo(tmpGatewayRequest, actItem.ownKeyStorePath, actItem.ownKeyPassword, getCharset());
				tmpGatewayRequest.put("signature", tmpStrRequest.get("signature"));
				tmpGatewayRequest.put("certId", tmpStrRequest.get("certId"));
				WriteBankLogLn2("发送隐含请求数据:\r\n" + tmpSignData);
				
				String submitGatewayUrl = bankFront.getUrl("gatewayPay");
				WriteBankLogLn2("银联网关支付提交服务地址:" + submitGatewayUrl);
				String tmpHtmlRet = AcpService.createAutoFormHtml(submitGatewayUrl, tmpGatewayRequest, getCharset());
				if (StringHelper.hasAnyChar(tmpHtmlRet)) {
					tmpRetMap.put("RespCode", "0");
					tmpRetMap.put("RespInfo", "签名成功");
					tmpRetMap.put("Result", tmpHtmlRet);
				} else {
					tmpRetMap.put("RespCode", "-1");
					tmpRetMap.put("RespInfo", "签名失败");
				}
			} else {
				tmpRetMap.put("RespCode", "0");
				tmpRetMap.put("RespInfo", "未找到账号:[" + ownAct + "]商户配置信息!");
				tmpRetMap.put("Result", "");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetMap.put("RespCode", "-1");
			tmpRetMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}
		
		return tmpRetMap;
	}

	/**
	 * 网关支付状态查询
	 * 
	 * @param p_req
	 *            查询请求对象
	 * @return 查询响应对象
	 * @throws Exception
	 *             支付状态查询异常
	 */
	@Override
	public RetAtsQueryOrder queryGWPay(ReqQueryOrder p_reqGateway) throws Exception {
		RetAtsQueryOrder retAts = new RetAtsQueryOrder();
		retAts.rdSeq = p_reqGateway.rdSeq;
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_reqGateway.ownAct, "gateway");
			if (actItem != null) {
				Map<String, String> tmpQryGatewayRequest = new HashMap<String, String>();

				tmpQryGatewayRequest.put("version", "5.1.0");
				tmpQryGatewayRequest.put("encoding", getCharset());
				tmpQryGatewayRequest.put("signMethod", "01");
				tmpQryGatewayRequest.put("txnType", "00");// 交易类型 00-默认
				tmpQryGatewayRequest.put("txnSubType", "00");// 交易子类型 默认00
				tmpQryGatewayRequest.put("bizType", "000201");// 业务类型
				tmpQryGatewayRequest.put("merId", actItem.merchantId);
				tmpQryGatewayRequest.put("accessType", "0");// 接入类型，商户接入固定填0，不需修改
				tmpQryGatewayRequest.put("orderId", p_reqGateway.rdSeq); 
				tmpQryGatewayRequest.put("txnTime", Format.DateTime14Format.format(p_reqGateway.getBgnDateTime()));
				WriteBankLogLn2("网关支付查询请求数据:\r\n" + SDKUtil.coverMap2String(tmpQryGatewayRequest));

				Map<String, String> tmpStrRequest = AcpService.signByCertInfo(tmpQryGatewayRequest, actItem.ownKeyStorePath, actItem.ownKeyPassword, getCharset());

				String submitQryGatewayUrl = bankFront.getUrl("gatewayPayQuery");
				WriteBankLogLn2("网关支付状态查询服务地址:" + submitQryGatewayUrl);
				
				Map<String, String> tmpMapRet = AcpService.post(tmpStrRequest, submitQryGatewayUrl, getCharset());
				WriteBankLogLn2("网关支付查询响应数据:\r\n" + SDKUtil.coverMap2String(tmpMapRet));
				if (tmpMapRet.size() > 0) {
					retAts.respCode = "0";
					retAts.respInfo = tmpMapRet.get("respMsg");
					
					String tmpRespCode = tmpMapRet.get("respCode");
					if ("00".equals(tmpRespCode)) {
						if (StringHelper.hasAnyChar(tmpMapRet.get("origRespMsg"))) {
							retAts.respInfo = tmpMapRet.get("origRespMsg");
						}
						retAts.outTransID = tmpMapRet.get("queryId");
						retAts.rdSeq = tmpMapRet.get("orderId");
						retAts.oppAct = tmpMapRet.get("accNo");// 客户账号
						retAts.transState = StringHelper.joinNotEmpty("+", tmpRespCode, tmpMapRet.get("origRespCode"));
						retAts.payInfoCode = tmpMapRet.get("origRespCode");
					} else {
						if ("34".equals(tmpRespCode)) {
							retAts.transState = tmpRespCode;
							retAts.payInfoCode = tmpRespCode;
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpMapRet.get("respMsg");
						}
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = "银行响应数据为空!";
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		return retAts;
	}
}
